import Vue from 'vue'
import { loadingType } from '../common/js/constant'

Vue.mixin({
  data() {
    return {
      btnLoading: false,
      tableLoading: false,
      pageNum: 1,
      pageSize: 10,
      total: 0,
      selection: []
    }
  },
  methods: {
    // 选择项改变触发事件
    selectionChange(selection) {
      this.selection = selection
    },
    // 路由跳转
    goToPath(url) {
      this.$router.push(url)
    },
    // 后退
    goToBack() {
      this.$router.go(-1)
    },
    // loading切面
    loadingAspect(fun, type) {
      if (type === loadingType.BUTTON) {
        this.btnLoading = true
        if (fun instanceof Promise) {
          setTimeout(() => {
            fun.finally(() => (this.btnLoading = false))
          }, 500)
        } else {
          this.btnLoading = false
        }
      }

      if (type === loadingType.TABLE) {
        this.tableLoading = true
        if (fun instanceof Promise) {
          setTimeout(() => {
            fun.finally(() => (this.tableLoading = false))
          }, 500)
        } else {
          this.tableLoading = false
        }
      }
    }
  }
})
